CC ?= ${CROSS_COMPILE}gcc
MODULE_PATH ?=/usr/lib/sipxmppserver
CONFIG_FILE_PATH ?= /etc/sipxmppserver.conf
LIBOBJS = base64.o ice_rtp.o memorywatcher.o sip.o xmppconnect.o dictionary.o iniparser.o micro_libxml.o logger.o user.o
PAP2V2FIRMWARE ?= pt-11.4.0-r060628-1.00.16-r060628.img
.PHONY:pap2v2_cook
all : main mod_user_config_file
%.o: %.c
	$(CC) ${CFLAGS} -c $< -o $@
sipxmppgate.o:
	$(CC) ${CFLAGS} -DMODULE_PATH=${MODULE_PATH} -c sipxmppgate.c  -o sipxmppgate.o
main : $(LIBOBJS) sipxmppgate.o
	$(CC) ${LIBOBJS} ${LDFLAGS} sipxmppgate.o -rdynamic -lpthread -lssl -lcrypto -ldl -o sipxmppserver
mod_user_config_file:
	$(CC) -DCONFIG_FILE_PATH=${CONFIG_FILE_PATH} ${CFLAGS} ${LDFLAGS} mod_user_config_file.c -fPIC -shared -o mod_user_config_file.so
mod_user_pap2_voice_config:
	$(CC) mod_user_pap2_voice_config.c ${CFLAGS} ${LDFLAGS} -lpthread -fPIC -shared -o mod_user_pap2_voice_config.so
clean:
	rm -rf *.o sipxmppserver *.so
install:
	install sipxmppserver /usr/sbin
	install -d ${MODULE_PATH}
	install mod_user_config_file.so ${MODULE_PATH}
	test -f pap2v2/${CONFIG_FILE_PATH} || install -m 640 sipxmppserver.conf ${CONFIG_FILE_PATH}
pap2v2: main mod_user_pap2_voice_config
pap2v2_cook:pap2v2
	@rm -f pap2v2/pap2v2.web pap2v2/pap2v2.flash pap2v2/kernel.bin pap2v2/rootfs.bin pap2v2/padder.bin pap2v2/sig.bin
	@test -f pap2v2/${PAP2V2FIRMWARE} || wget http://httpconfig.vonage.net/${PAP2V2FIRMWARE} -O - > pap2v2/${PAP2V2FIRMWARE} 2>/dev/null
	@dd if=pap2v2/${PAP2V2FIRMWARE} of=pap2v2/kernel.bin bs=4k skip=4 count=120 2>/dev/null
	@dd if=pap2v2/${PAP2V2FIRMWARE} of=pap2v2/rootfs.bin bs=4k skip=124 count=654 2>/dev/null
	@dd if=pap2v2/${PAP2V2FIRMWARE} of=pap2v2/padder.bin bs=4k skip=778 count=138 2>/dev/null
	@dd if=pap2v2/${PAP2V2FIRMWARE} of=pap2v2/sig.bin bs=1 skip=3751936 count=149 2>/dev/null
	@test -f pap2v2/squashfs-root || rm -rf pap2v2/squashfs-root
	- unsquashfs-lzma -dest pap2v2/squashfs-root pap2v2/rootfs.bin >/dev/null 2>/dev/null
	@echo applying patch
	@tar xzf pap2v2/pap2v2na-1.00.16_patch0.5b.tgz -C pap2v2/squashfs-root > /dev/null
	@cp pap2v2/html pap2v2/squashfs-root/usr/www_safe -R
	@cp pap2v2/rcS pap2v2/squashfs-root/etc/init.d/rcS
	@cp pap2v2/versions pap2v2/squashfs-root/etc/versions
	@install -m 755 sipxmppserver pap2v2/squashfs-root/usr/sbin
	@install -d pap2v2/squashfs-root/usr/lib/sipxmppserver
	@install -m 755 mod_user_pap2_voice_config.so pap2v2/squashfs-root/usr/lib/sipxmppserver
	mksquashfs-lzma pap2v2/squashfs-root pap2v2/pap2v2.sqfs -noappend > /dev/null
	@cat pap2v2/kernel.bin pap2v2/pap2v2.sqfs pap2v2/padder.bin pap2v2/padder.bin > pap2v2/pap2v2.stage1
	@dd if=pap2v2/pap2v2.stage1 of=pap2v2/pap2v2.stage2 bs=4k count=912 2> /dev/null
	@cat pap2v2/head-w.bin pap2v2/pap2v2.stage2 pap2v2/sig.bin >> pap2v2/pap2v2.web
	@tichksum pap2v2/pap2v2.web > /dev/null
	@cat pap2v2/head-f.bin pap2v2/pap2v2.stage2 pap2v2/sig.bin >> pap2v2/pap2v2.flash
	@rm pap2v2/pap2v2.sqfs pap2v2/pap2v2.stage1 pap2v2/pap2v2.stage2 pap2v2/kernel.bin pap2v2/rootfs.bin pap2v2/padder.bin pap2v2/sig.bin
	@rm -rf pap2v2/squashfs-root
